package com.view.lyric
{

	/**
	 * lrc 文件解析
	 * 
	 */	
	public class LRCLyricParser implements LyricParser
	{
		private var lrcList:Array = [];
		private var lrcRunList:Array = [];
		
		public function LRCLyricParser()
		{
			super();
		}
		
		public function parse(data:String):Array
		{
			var listarray:Array = data.split("\n");
			var reg:RegExp=/\[[0-5][0-9]:[0-5][0-9].[0-9][0-9]\]/g;
			for (var i:int=0; i<listarray.length; i++) 
			{
				var info:String=listarray[i];
				var len:int=info.match(reg).length;
				var timeAry:Array=info.match(reg);
				var lyric:String=info.substr(len*10);
				for (var k:int=0; k<timeAry.length; k++) 
				{
					var obj:Object=new Object();
					var ctime:String=timeAry[k];
					var ntime:Number=Number(ctime.substr(1,2))*60+Number(ctime.substr(4,5));
					obj.timer=ntime*1000;
					obj.lyric=lyric;
					lrcList.push(obj);
				}
			}
			lrcList.sort(compare);
			return lrcList;
		}
		
		private function compare(paraA:Object,paraB:Object):int 
		{
			if (paraA.timer>paraB.timer) {
				return 1;
			}
			if (paraA.timer<paraB.timer) {
				return -1;
			}
			return 0;
		}
		
		public function getRunList(list:Array):Array
		{
			var temp:Object;
			for (var i:int = 0; i < list.length; i++) 
			{
				temp = {
					timer:list[i].timer - (i>0?list[i-1].timer:0),
					width:String(list[i].lyric).length
				};
				lrcRunList.push( temp );
			}
			return lrcRunList;
		}
		
		/** bool 表示是否改变字幕上下的位置 **/
		public function getTxtByPosition(now:Number):Object
		{
			var obj:Object;
			for (var i:int=1,j:int=lrcList.length; i<j; i++) 
			{
				if (now < lrcList[i].timer) 
				{
					obj = {
						currentLyric: lrcList[i-1].lyric,
						nextLyric:lrcList[i].lyric,
						bool:i%2==0
					};
					break;
				}
				obj = {
					currentLyric:lrcList[j-1].lyric,
					nextLyric:j<lrcList.length?lrcList[j].lyric:"",
					bool:j%2==0
				}
			}
			return obj;
		}
		
		public function getLengthByPosition(now:Number):Number
		{
			var len:Number;
			for (var k:int = 0; k < lrcList.length-1; k++) 
			{
				if(now < lrcList[k+1].timer)
				{
					len = (now - lrcList[k].timer)/(lrcRunList[k+1].timer==0?1:lrcRunList[k+1].timer); 
					len *= lrcList[k].lyric.length*30;
					return len;
				}
			}
			return 0;
		}

		
		public function seek(now:Number):void
		{
//			ModelLocator.getInstance().paiMaker.videoPlayer.maskImg.width = 30*getLengthByPosition(now);
		}
		
		public function topSeek(now:Number):void
		{
//			ModelLocator.getInstance().paiMaker.videoPlayer.topMaskImg.width = ModelLocator.getInstance().paiMaker.videoPlayer.topTxt.width;
//			ModelLocator.getInstance().paiMaker.videoPlayer.topMaskImg.width = getLengthByPosition(now);
//			ModelLocator.getInstance().paiMaker.videoPlayer.bottomMaskImg.width = 0;
//			trace("top: ", ModelLocator.getInstance().paiMaker.videoPlayer.topMaskImg.width);
		}
		
		public function bottomSeek(now:Number):void
		{
//			ModelLocator.getInstance().paiMaker.videoPlayer.bottomMaskImg.width = ModelLocator.getInstance().paiMaker.videoPlayer.bottomTxt.width;
//			ModelLocator.getInstance().paiMaker.videoPlayer.bottomMaskImg.width = getLengthByPosition(now);
//			ModelLocator.getInstance().paiMaker.videoPlayer.topMaskImg.width = 0;
//			trace("bottom: ",ModelLocator.getInstance().paiMaker.videoPlayer.bottomMaskImg.width);
		}
		
		
	}
}